﻿/*
 
    This file is part of AnkhBG.

    AnkhBG is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    AnkhBG is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with AnkhBG.  If not, see <http://www.gnu.org/licenses/>.
 
 */

using System;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using Glider.Common.Objects;

namespace AnkhBG
{
    public class Main : IDisposable
    {
        #region Private Fields

        private static Main _Instance = null;
        private bool IsDisposed = false;
        private bool _running = false;

        #endregion

        #region Public Fields

        public static Main __Instance
        {
            get { return _Instance; }
        }

        public static Main Instance
        {
            get
            {
                if (_Instance == null)
                    _Instance = new Main();
                return _Instance;
            }
        }

        public MainForm Window = new MainForm();
        public Statistics Stats = new Statistics();
        public QueueManager Queue = new QueueManager();
        public Configuration Config = new Configuration();
        public Runner Run = new Runner();

        public static string version = "0.5 beta";

        public static string path { get { return Path.GetDirectoryName(Assembly.GetEntryAssembly().Location); } }

        public bool error = false;

        public DateTime starttime;

        #endregion
       
        
        public bool running
        {
            get
            {
                return _running;
            }
            set
            {
                _running = value;
                if (value == true)
                { // update the window
                    error = false;

                    if (Window != null && !Window.IsDisposed)
                    {
                        Window.Log("Started");

                        Window.StartStopButton.Image = global::AnkhBG.Properties.Resources.control_stop_blue;
                        Window.StartStopButton.Text = "Stop";
                        Window.StartStopButton.ToolTipText = "Stop";
                        Window.toolStrip1.ResumeLayout(false);

                        Window.StatusText.Image = global::AnkhBG.Properties.Resources.tick;
                        Window.StatusText.Text = "Running";

                        Stats.ResetSession();
                        starttime = DateTime.Now;
                    }

                    if (Run != null)
                        Run.Start();
                }
                else
                {
                    if (Run != null)
                        Run.Stop();

                    if (GContext.Main != null && GContext.Main.IsGliding)
                        GContext.Main.KillAction("AnkhBG: Stopped", false);

                    if (Window != null && !Window.IsDisposed)
                    {
                        Window.Log("Stopped");

                        Window.StartStopButton.Image = global::AnkhBG.Properties.Resources.control_play_blue;
                        Window.StartStopButton.Text = "Start";
                        Window.StartStopButton.ToolTipText = "Start";
                        Window.toolStrip1.ResumeLayout(false);

                        if (!error)
                        {
                            Window.StatusText.Image = global::AnkhBG.Properties.Resources.tick;
                            Window.StatusText.Text = "Stopped";
                        }
                    }
                }
            }
        }


        private Main()
        {
            Environment.CurrentDirectory = path;

            Window.Show();

            if (Settings.Instance.RandomTitles == "true")
                Window.Text = Glider.Common.Objects.GContext.Main.GetRandomString();

            Window.Log("Welcome to AnkhBG " + version + " - The lightweight Honor-Leeching-System");
        }

        public void Dispose()
        {
            Dispose(true);
        }

        protected void Dispose(bool Disposing)
        {
            try
            {
                if (GContext.Main != null && GContext.Main.IsGliderRunning)
                    GContext.Main.Debug("AnkhBG: Call: Main::Dispose("+Disposing.ToString()+");");

                if (!IsDisposed)
                {
                    if (Window != null)
                        Window.Dispose();
                    if (Run != null)
                    {
                        Run.Stop();
                        Run = null;
                    }

                    IsDisposed = true;
                }
            }
            catch (Exception)
            { }
        }

        ~Main()
        {
            try
            {
                if (GContext.Main != null && GContext.Main.IsGliderRunning)
                    GContext.Main.Debug("AnkhBG: Call: Main::~Main();");
                Dispose(false);
            }
            catch (NullReferenceException e)
            {
                if (GContext.Main != null && GContext.Main.IsGliderRunning)
                    GContext.Main.Debug("AnkhBG: Main::~Main()::__NullReferenceException: " + e.ToString());
            }
            catch (Exception e)
            {
                if (GContext.Main != null && GContext.Main.IsGliderRunning)
                    GContext.Main.Debug("AnkhBG: Main::~Main()::Exception: " + e.ToString());
            }
        }
    }
}